/************************************************************************************/
/*  Program name CAPGLIMP_JE_LINES.sql                                        	    */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 24 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_JE_LINES';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';
	
	lv_FileName		VARCHAR2(100)	:=	'gl_je_lines.log';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;
	lv_counter		VARCHAR(100);
	lv_counter2		VARCHAR(100);
	
	CURSOR Cur_F IS
	SELECT	H.JE_CATEGORY, H.JE_SOURCE, C1.*
	FROM	GL_JE_HEADERS	H,
			--GL_JE_LINES		L,
			CRST_GL_JE_LINES C1
	WHERE	H.JE_HEADER_ID	=	C1.JE_HEADER_ID
	AND		H.JE_CATEGORY	IS	NOT	NULL
	AND		H.JE_SOURCE		IS	NOT	NULL;
	
	
	lv_Next_Seq_ID	NUMBER;
	lv_COAID		NUMBER;
	Source			VARCHAR2(25);
	Category		VARCHAR2(25);
	R2				VARCHAR2(240);
	R3				VARCHAR2(240);
	R5				VARCHAR2(240);
	R7				VARCHAR2(240);
	R9				VARCHAR2(240);
	
BEGIN
	
	lv_counter := 0;

	FOR C1 IN Cur_F LOOP
		BEGIN
			
			lv_counter := lv_counter + 1;
     		
		    lv_UTL_File := UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		    UTL_FILE.PUT_LINE(lv_UTL_File, lv_counter);
			UTL_FILE.FClose(lv_UTL_File);
			
			if lv_counter > 500 then
				commit;
			end if;
			
			lv_COAID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);
			
			R2	:=	C1.REFERENCE_2;
			R3	:=	C1.REFERENCE_3;
			R5	:=	C1.REFERENCE_5;
			R7	:=	C1.REFERENCE_7;
			R9	:=	C1.REFERENCE_9;
			
			/*SELECT	H.JE_CATEGORY, H.JE_SOURCE
			INTO	Category, Source
			FROM	GL_JE_HEADERS	H,
					GL_JE_LINES		L
			WHERE	H.JE_HEADER_ID	=	C1.JE_HEADER_ID
			AND		H.JE_CATEGORY	IS	NOT	NULL
			AND		H.JE_SOURCE		IS	NOT	NULL;*/
			
			Category := C1.JE_CATEGORY;
			Source := C1.JE_SOURCE;
			
			IF		Category	=	'Purchase Invoices'	
			AND		Source		=	'Payables'			THEN
				R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				R7	:=	TO_CHAR(CAPBRIMP_MAPPING.GetSOBIDMapping(TO_NUMBER(C1.REFERENCE_7)));

			ELSIF	Category	=	'Payments'	
			AND		Source		=	'Payables'			THEN
				IF	R2	IS NOT NULL	THEN
					R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				END IF;
				R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetCheckIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				R7	:=	TO_CHAR(CAPBRIMP_MAPPING.GetSOBIDMapping(TO_NUMBER(C1.REFERENCE_7)));
				IF	R9	IS NOT NULL	THEN
					R9	:=	TO_CHAR(CAPBRIMP_MAPPING.GetInvPaymentIDMapping(TO_NUMBER(C1.REFERENCE_9)));
				END IF;

			ELSIF	Category	=	'Sales Invoices'	
			AND		Source		=	'Receivables'			THEN
				R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustTRXIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustTRXLnDistIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				R7	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustomerIDMapping(TO_NUMBER(C1.REFERENCE_7)));

			ELSIF	Category	=	'Trade Receipts'	
			AND		Source		=	'Receivables'			THEN
				IF	C1.REFERENCE_9	=	'TRADE_CASH'	THEN	
					R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE_2,1,4))))
							||'C'||
							TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptHistIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE_2,-4))));
				ELSE
					R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE_2,1,4))))
							||'C'||
							TO_CHAR(CAPBRIMP_MAPPING.GetARReceivableAppIDMapping(TO_NUMBER(SUBSTR(C1.REFERENCE_2,-4))));
				END IF;
				R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARLineIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				R7	:=	TO_CHAR(CAPBRIMP_MAPPING.GetRACustomerIDMapping(TO_NUMBER(C1.REFERENCE_7)));

			ELSIF	Category	=	'Misc Receipts'	
			AND		Source		=	'Receivables'			THEN
				R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARCashReceiptIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetARLineIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				--IF	C1.REFERENCE29	=	'MISC_CASH'	THEN	
				IF	C1.REFERENCE_9	=	'MISC_CASH'	THEN	
					R5	:=	C1.REFERENCE_5;
				ELSE
					R5	:=	C1.REFERENCE_5;
				END IF;
			
			ELSIF	(	Category	=	'Purchases'
					OR	Category	=	'Accrual'
					OR	Category	=	'Requisitions')
			AND		Source		=	'Purchasing'	THEN
				IF	C1.REFERENCE_1	=	'PO'	THEN
					R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOHeaderIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				ELSIF	C1.REFERENCE_1	=	'REQ'	THEN
					R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOReqHeaderIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				END IF;
				IF	C1.REFERENCE_1	=	'PO'	THEN
					R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPODistIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				ELSIF	C1.REFERENCE_1	=	'REQ'	THEN
					R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOReqDistIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				END IF;
			
			ELSIF	Category	=	'Receiving'
			AND		Source		=	'Purchasing'	THEN
				R2	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPOHeaderIDMapping(TO_NUMBER(C1.REFERENCE_2)));
				R3	:=	TO_CHAR(CAPBRIMP_MAPPING.GetPODistIDMapping(TO_NUMBER(C1.REFERENCE_3)));
				R5	:=	C1.REFERENCE_5;
			END IF;
			
			INSERT INTO GL_JE_LINES
			VALUES
			(
			CAPBRIMP_MAPPING.GetJEHeaderIDMapping(C1.JE_HEADER_ID),
			C1.JE_LINE_NUM,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),
			CAPBRIMP_COMMON.GetSegment(lv_COAID,C1.CODE_COMB_ID_D),
			C1.PERIOD_NAME,
			C1.EFFECTIVE_DATE,
			C1.STATUS,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.LAST_UPDATE_LOGIN,
			C1.ENTERED_DR,
			C1.ENTERED_CR,
			C1.ACCOUNTED_DR,
			C1.ACCOUNTED_CR,
			C1.DESCRIPTION,
			C1.LINE_TYPE_CODE,
			CAPBRIMP_MAPPING.RenameStr(substr(C1.REFERENCE_1,1,4),C1.REFERENCE)
			R2,
			R3,
			C1.REFERENCE_4,
			R5,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.CONTEXT,
			C1.CONTEXT2,
			C1.INVOICE_DATE,
			C1.TAX_CODE,
			C1.INVOICE_IDENTIFIER,
			C1.INVOICE_AMOUNT,
			C1.NO1,
			C1.STAT_AMOUNT,
			C1.IGNORE_RATE_FLAG,
			C1.CONTEXT3,
			C1.USSGL_TRANSACTION_CODE,
			CAPBRIMP_COMMON.GetDocSeqID(C1.DOC_SEQ_NAME),
			C1.CONTEXT4,
			C1.SUBLEDGER_DOC_SEQUENCE_VALUE,
			C1.REFERENCE_6,
			R7,
			C1.GL_SL_LINK_ID,
			C1.GL_SL_LINK_TABLE,
			C1.REFERENCE_8,
			R9,
			C1.REFERENCE_10,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.JGZZ_RECON_STATUS,
			C1.JGZZ_RECON_DATE,
			C1.JGZZ_RECON_ID,
			C1.JGZZ_RECON_REF,
			C1.JGZZ_RECON_CONTEXT,
			C1.TAXABLE_LINE_FLAG,
			C1.TAX_TYPE_CODE,
			C1.TAX_CODE_ID,
			C1.TAX_ROUNDING_RULE_CODE,
			C1.AMOUNT_INCLUDES_TAX_FLAG,
			C1.TAX_DOCUMENT_IDENTIFIER,
			C1.TAX_DOCUMENT_DATE,
			C1.TAX_CUSTOMER_NAME,
			C1.TAX_CUSTOMER_REFERENCE,
			C1.TAX_REGISTRATION_NUMBER,
			C1.TAX_LINE_FLAG,
			C1.TAX_GROUP_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/